//
//  CZEmoticonCell.swift
//  weibo
//
//  Created by 丁蒙 on 16/7/5.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZEmoticonCell: UICollectionViewCell {
    //存放按钮的数组
    var emoticonButtons: [CZEmoticonButton] = [CZEmoticonButton]()
    
    var emoticons: [CZEmoticons]?{
        didSet {
            for button in emoticonButtons {
                button.hidden = true
            }
            
            for (i,emoticon) in emoticons!.enumerate() {
                
                let button = emoticonButtons[i]
                
                button.emoticon = emoticon
                
                button.hidden = false
                
                if emoticon.isEmoji {
                    let tit = ((emoticon.code ?? "") as NSString).emoji()
                    
                    button.setTitle(tit, forState: UIControlState.Normal)
                    button.setImage(nil, forState: UIControlState.Normal)
                } else {
                    let path = emoticon.path ?? ""
                    let png = emoticon.png ?? ""
                    
                    let name = path + png
                
                    let image = UIImage(named: name, inBundle: CZEmoticonTools.sharedEmoticonTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addChildButtons()
        
        contentView.addSubview(backButton)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let W = (SCREENW - 10) / CGFloat(EmoticonMaxCol)
        let H = (216 - 37 - 20) / CGFloat(EmoticonMaxRow)
        
        for (i, button) in emoticonButtons.enumerate() {
            
            let X = i % EmoticonMaxCol
            let Y = i / EmoticonMaxCol
            
            button.frame = CGRect(x: CGFloat(X) * W + 5, y: CGFloat(Y) * H, width: W, height: H)
        }
        
        backButton.frame = CGRect(x: SCREENW - 5 - W, y: 216 - 2 * H, width: W, height: H)
    }
    // MARK: - 监听回退按钮点击事件
    @objc private func backButtonClick() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(BackButtonNoti, object: nil)
        
    }
    // MARK: - 表情按钮点击事件
    @objc private func emoticonButtonClick(emoticonButton: CZEmoticonButton) {
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNoti, object: emoticonButton.emoticon)
    }
    
    // MARK: - 创建button的公共方法
    private func addChildButtons() {
        
        for _ in 0..<EmoticonMaxCount {
            let btn: CZEmoticonButton = CZEmoticonButton()
            
            btn.addTarget(self, action: #selector(CZEmoticonCell.emoticonButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
            btn.titleLabel?.font = UIFont.systemFontOfSize(34)
            emoticonButtons.append(btn)
            
            contentView.addSubview(btn)
            
        }
    }
    //删除按钮
    private lazy var backButton: UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(CZEmoticonCell.backButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
       
        return btn
    }()
}
